package bigoo.common;

/**
 * Represents a peer information entry at the <code>PeerDirectory</code>.
 * 
 * @author Danilo Penna Queiroz <dpenna.queiroz@gmail.com>
 */
public class PeerEntry extends PeerInfo implements Comparable<PeerEntry> {

    private long leaseTime;

    public PeerEntry() {
	this(null);
    }

    public PeerEntry(String peerID) {
	super(peerID,null, -1);
    }

    public void setAddress(String address) {
	this.address = address;
    }

    public void setPort(int port) {
	this.port = port;
    }

    public long getLeaseTime() {
	return leaseTime;
    }

    public void setLeaseTime(long leaseTime) {
	this.leaseTime = leaseTime;
    }

    @Override
    public int compareTo(PeerEntry o) {
	return (int) (this.leaseTime - o.leaseTime);
    }

    @Override
    public boolean equals(Object obj) {
	if (obj instanceof PeerEntry)
	    return false;

	PeerEntry other = (PeerEntry) obj;
	return this.leaseTime == other.leaseTime;
    }

    @Override
    public int hashCode() {
	return new Long(leaseTime).hashCode();
    }

    @Override
    public PeerEntry clone() {
	PeerEntry clone = new PeerEntry(this.peerID);
	clone.setAddress(this.address);
	clone.setPort(this.port);
	clone.setLeaseTime(this.leaseTime);
	return clone;
    }
}
